package com.moxiao.yfjh.module.blind.service.notifymessage;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.module.blind.controller.admin.notifymessage.vo.*;
import com.moxiao.yfjh.module.blind.convert.notifymessage.BlindNotifyMessageConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.notifymessage.BlindNotifyMessageDO;
import com.moxiao.yfjh.module.blind.dal.mysql.notifymessage.BlindNotifyMessageMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.blind.enums.ErrorCodeConstants.NOTIFY_MESSAGE_NOT_EXISTS;

/**
 * 相亲通知消息 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class BlindNotifyMessageServiceImpl implements BlindNotifyMessageService {

    @Resource
    private BlindNotifyMessageMapper blindNotifyMessageMapper;

    @Override
    public Long createNotifyMessage(BlindNotifyMessageCreateReqVO createReqVO) {
        // 插入
        BlindNotifyMessageDO notifyMessage = BlindNotifyMessageConvert.INSTANCE.convert(createReqVO);
        blindNotifyMessageMapper.insert(notifyMessage);
        // 返回
        return notifyMessage.getId();
    }

    @Override
    public void updateNotifyMessage(BlindNotifyMessageUpdateReqVO updateReqVO) {
        // 校验存在
        validateNotifyMessageExists(updateReqVO.getId());
        // 更新
        BlindNotifyMessageDO updateObj = BlindNotifyMessageConvert.INSTANCE.convert(updateReqVO);
        blindNotifyMessageMapper.updateById(updateObj);
    }

    @Override
    public void oneKeyClearNotifyMessage(Long userId) {
        blindNotifyMessageMapper.oneKeyClearNotifyMessage(userId);
    }

    @Override
    public void deleteNotifyMessage(Long id) {
        // 校验存在
        validateNotifyMessageExists(id);
        // 删除
        blindNotifyMessageMapper.deleteById(id);
    }

    private void validateNotifyMessageExists(Long id) {
        if (blindNotifyMessageMapper.selectById(id) == null) {
            throw exception(NOTIFY_MESSAGE_NOT_EXISTS);
        }
    }

    @Override
    public BlindNotifyMessageDO getNotifyMessage(Long id) {
        return blindNotifyMessageMapper.selectById(id);
    }

    @Override
    public List<BlindNotifyMessageDO> getNotifyMessageList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return blindNotifyMessageMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BlindNotifyMessageDO> getNotifyMessagePage(BlindNotifyMessagePageReqVO pageReqVO) {
        return blindNotifyMessageMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BlindNotifyMessageDO> getNotifyMessageList(BlindNotifyMessageExportReqVO exportReqVO) {
        return blindNotifyMessageMapper.selectList(exportReqVO);
    }

    @Override
    public List<BlindNotifyMessageRespVO>  getNotifyMessageList(Long loginUserId) {
        List<String> messageTypeList = blindNotifyMessageMapper.getMessageType();
        List<BlindNotifyMessageRespVO> blindNotifyMessageRespVOS = new ArrayList<>();
        messageTypeList.forEach(message ->{
            BlindNotifyMessagePageReqVO reqVO = new BlindNotifyMessagePageReqVO();
            reqVO.setMessageType(message);
            reqVO.setPageSize(1);
            reqVO.setPageNo(1);
            reqVO.setUserId(String.valueOf(loginUserId));
            PageResult<BlindNotifyMessageDO> blindNotifyMessageDOPageResult = blindNotifyMessageMapper.selectPage(reqVO);
            PageResult<BlindNotifyMessageRespVO> voPageResult = BlindNotifyMessageConvert.INSTANCE.convertPage(blindNotifyMessageDOPageResult);
            if (Objects.nonNull(voPageResult)){
                List<BlindNotifyMessageRespVO> list = voPageResult.getList();
                if (CollectionUtils.isNotEmpty(list)){
                    blindNotifyMessageRespVOS.add(list.get(0));
                }
            }
        });
       return blindNotifyMessageRespVOS;
    }


    private String messageName(String messageType){
        switch (messageType) {
            case "1": return "提现通知";
            case "2": return "牵线通知";
            case "3": return "打招呼通知";
            case "4": return "活动通知";
            case "5": return "强制配对通知";
            case "6": return "订单通知";
            case "7": return "系统通知";
            case "8": return "喜欢通知";
            default:
               return "系统通知";
        }
    }

}
